// service/MovieService.java
package main.java.com.cinema.service;

import main.java.com.cinema.model.Movie;
import main.java.com.cinema.dao.MovieDAO;
import main.java.com.cinema.util.Logger;

import java.util.List;
import java.util.UUID;

public class MovieService {
    private MovieDAO movieDAO = new MovieDAO();
    
    public List<Movie> listAllMovies() {
        return movieDAO.findAll();
    }
    
    public Movie findMovieById(String movieId) {
        return movieDAO.findById(movieId);
    }
    
    public List<Movie> searchMovies(String title, String director, String actor) {
        return movieDAO.searchMovies(title, director, actor);
    }
    
    public boolean addMovie(String operator, String title, String director, String[] actors, 
                           String synopsis, int duration, double rating) {
        // 检查参数有效性
        if (title == null || title.trim().isEmpty()) {
            return false;
        }
        
        // 创建新电影
        String movieId = "M" + UUID.randomUUID().toString().substring(0, 8).toUpperCase();
        Movie movie = new Movie(movieId, title, director, actors, synopsis, duration, rating);
        
        boolean result = movieDAO.save(movie);
        if (result) {
            Logger.log("添加影片", "添加影片: " + title, operator);
        }
        return result;
    }
    
    public boolean updateMovie(String operator, String movieId, String title, String director, 
                              String[] actors, String synopsis, int duration, double rating) {
        Movie movie = movieDAO.findById(movieId);
        if (movie == null) {
            return false;
        }
        
        // 更新电影信息
        movie.setTitle(title);
        movie.setDirector(director);
        movie.setActors(actors);
        movie.setSynopsis(synopsis);
        movie.setDuration(duration);
        movie.setRating(rating);
        
        boolean result = movieDAO.update(movie);
        if (result) {
            Logger.log("修改影片", "修改影片: " + title, operator);
        }
        return result;
    }
    
    public boolean deleteMovie(String operator, String movieId) {
        Movie movie = movieDAO.findById(movieId);
        if (movie == null) {
            return false;
        }
        
        boolean result = movieDAO.delete(movieId);
        if (result) {
            Logger.log("删除影片", "删除影片: " + movie.getTitle(), operator);
        }
        return result;
    }
}
